package com.xjscrm.console.chat.core.impl;

import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.chat.cache.CacheHolder;
import com.xjscrm.console.chat.core.ChatBaseMsgHandler;
import com.xjscrm.console.chat.enums.ChatMsgType;
import com.xjscrm.console.chat.frame.recv.RHeartBeatMsg;
import com.xjscrm.console.chat.frame.send.SHeartBeatResultMsg;
import com.xjscrm.console.chat.frame.send.SResultMsg;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author mingyang.chen create in 2024/3/12 09:41
 */
@Component
@Slf4j
public class ChatHeartBeatMsgHandler extends ChatBaseMsgHandler<RHeartBeatMsg> {

    @Autowired
    private CacheHolder cacheHolder;

    @Override
    public SResultMsg doHandle(RHeartBeatMsg content, ChannelHandlerContext ctx) {

        try {
            Long merId = content.getMerId();
            Long staffId = content.getStaffId();
            //
            cacheHolder.refresh(merId, staffId);
            return SHeartBeatResultMsg.success(content, "心跳成功", ChatMsgType.s_heart_beat);
        } catch (Exception e) {
            log.error("心跳失败", e);
            throw new BusinessException("心跳失败");
        }
    }


    @Override
    public Class<RHeartBeatMsg> msgClass() {
        return RHeartBeatMsg.class;
    }

    @Override
    public ChatMsgType msgType() {
        return ChatMsgType.heart_beat;
    }
}
